package com.t249.bdqn.renting.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.t249.bdqn.renting.dao.*;
import com.t249.bdqn.renting.entity.*;
import com.t249.bdqn.renting.util.TxCloudSmsUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping(value = "/retAddress")
public class RetAddressController {

    Integer sid = 1;

    //获取的验证码
    private Integer verifyCode;

    @Resource
    private RetAddressDao retAddressdao;

    @Resource
    private RetRegionDao retRegionDao;

    @Resource
    private RetImgDao retImgDao;

    @Resource
    private RetRentalInformationDao retRentalInformationDao;

    @Resource
    private RetPropertiesDao retPropertiesDao;

    @Resource
    private TxCloudSmsUtil txCloudSmsUtil;

    @Resource
    private RetUserDao retUserDao;

    @Resource
    private SysBookingDao sysBookingDao;

    @Resource
    private RetCollectionDao retCollectionDao;

    //跳转到中介租房网首页
    @RequestMapping(value = "/index")
    public String index(Model model, RetAddress retAddress){
        //无条件情况下获取所以的城市名称
        List<RetAddress> retAddressList = retAddressdao.selectList(null);
        if(retAddress.getId() != null){
            //根据id获取相对应的城市名称
            sid = retAddress.getId();
            RetAddress retAddressId = retAddressdao.selectOne(new QueryWrapper<RetAddress>().eq("id",retAddress.getId()));

            //根据id获取相对应的区域
            List<RetRegion> retRegionList = retRegionDao.selectList(new QueryWrapper<RetRegion>().eq("address_Id",retAddress.getId()));

            //根据id来查询相关城市里的房源信息
            List<Map<String,Object>> retRentalInformationList = retRentalInformationDao.ChengshiSelect(retAddress.getId());

            model.addAttribute("retRegionList",retRegionList);
            model.addAttribute("cityName",retAddressId.getCityName());
            model.addAttribute("retRentalInformationList",retRentalInformationList);
        }else{
            sid = 1;
            //没有id就默认为深圳
            List<RetRegion> retRegionList = retRegionDao.selectList(new QueryWrapper<RetRegion>().eq("address_Id",1));

            //没有id就默认为显示深圳的房源
            List<Map<String,Object>> retRentalInformationList = retRentalInformationDao.ChengshiSelect(1);

            model.addAttribute("retRegionList",retRegionList);
            model.addAttribute("cityName","深圳");
            model.addAttribute("retRentalInformationList",retRentalInformationList);
        }
        model.addAttribute("retAddressList",retAddressList);
        return "index.html";
    }

    //动态的获取板块“猜你喜欢”中选定的区域房源
    @RequestMapping(value = "/diquTiao")
    @ResponseBody
    public Object diquTiao(RetAddress retAddress){
        List<Map<String,Object>> retRentalInformationList1 = retRentalInformationDao.ShiquSelect(retAddress.getId());
        return JSONArray.toJSONString(retRentalInformationList1);
    }



    //跳转到房屋详情页面上
    @RequestMapping(value = "/houseDetails")
    public String houseDetails(Model model,Integer id){
        //无条件情况下获取所以的城市名称
        List<RetAddress> retAddressList = retAddressdao.selectList(null);

        //根据点击房源传过来的id来查询相对应的房源图片
        List<RetImg> retImgList = retImgDao.selectList(new QueryWrapper<RetImg>().eq("ret_rental_Id",id));

        //根据点击房源传过来的id来查询相对应的房源基本信息
        List<Map<String,Object>> retRentalInformationList = retRentalInformationDao.FanyuanSelect(id);

        //猜你喜欢部分
        List<Map<String,Object>> retRentalInformationCai = retRentalInformationDao.ChengshiSelect(sid);
        if(sid != null && sid !=0){
            //根据id获取相对应的这一条数据为了获取城市id
            RetRentalInformation retRentalInformation1=retRentalInformationDao.selectOne(new QueryWrapper<RetRentalInformation>().eq("ret_id",sid));
            //根据id获取相对应的城市名称
            RetAddress retAddressId = retAddressdao.selectOne(new QueryWrapper<RetAddress>().eq("id",retRentalInformation1.getAddressId()));
            //根据id获取相对应的区域
            List<RetRegion> retRegionList = retRegionDao.selectList(new QueryWrapper<RetRegion>().eq("address_Id",retRentalInformation1.getAddressId()));
            model.addAttribute("retRegionList",retRegionList);
            model.addAttribute("cityName",retAddressId.getCityName());
            model.addAttribute("id",id);
        }
        model.addAttribute("retImgList",retImgList);
        model.addAttribute("retAddressList",retAddressList);
        model.addAttribute("retRentalInformationList",retRentalInformationList);
        model.addAttribute("retRentalInformationCai",retRentalInformationCai);
        model.addAttribute("id",id);
        return "HouseDetails.html";
    }



    //跳转到找房中心
    @RequestMapping(value = "/searchList")
    public String searchList(String id,Integer money,String leaseOut,Model model){
        //无条件情况下获取所以的城市名称
        List<RetAddress> retAddressList = retAddressdao.selectList(null);
        //查询出所以的收藏状态为0:收藏中....
        List<RetCollection> retCollectionList = retCollectionDao.selectList(new QueryWrapper<RetCollection>().eq("ret_state",0));
        List<RetRentalInformation> retRentalInformationList = null;
        List<RetRentalInformation> fanYuancount =  null;
        if(sid != null && sid != 0 || id != "" && id != null || money != 0 && money != null || leaseOut != "" && leaseOut != null){
            //根据id获取相对应的城市名称
            RetAddress retAddressId = retAddressdao.selectOne(new QueryWrapper<RetAddress>().eq("id",sid));

            //根据id获取相对应的区域
            List<RetRegion> retRegionList = retRegionDao.selectList(new QueryWrapper<RetRegion>().eq("address_Id",sid));

            if(id == null){
                id = "";
            }
            if(leaseOut == null){
                leaseOut = "";
            }
            //获取总楼盘
            if(money != null && money != 0){
                retRentalInformationList = retRentalInformationDao.selectList(new QueryWrapper<RetRentalInformation>().like("address_Id",sid).like("region_Id",id).lt("money",money).like("lease_out",leaseOut).groupBy("region_Id"));
            }else{
                retRentalInformationList = retRentalInformationDao.selectList(new QueryWrapper<RetRentalInformation>().like("address_Id",sid).like("region_Id",id).like("lease_out",leaseOut).groupBy("region_Id"));
            }

            //获取总房源
            if(money != null && money != 0){
                fanYuancount = retRentalInformationDao.selectList(new QueryWrapper<RetRentalInformation>().like("address_Id",sid).like("region_Id",id).lt("money",money).like("lease_out",leaseOut));
            }else{
                fanYuancount = retRentalInformationDao.selectList(new QueryWrapper<RetRentalInformation>().like("address_Id",sid).like("region_Id",id).like("lease_out",leaseOut));
            }

            model.addAttribute("retRegionList",retRegionList);
            model.addAttribute("cityName",retAddressId.getCityName());
            model.addAttribute("cityNameId",retAddressId.getId());
            model.addAttribute("retRentalInformationList",retRentalInformationList.size());
            model.addAttribute("fanYuancount",fanYuancount.size());
            model.addAttribute("id",id);
            model.addAttribute("money",money);
            model.addAttribute("List",ccc(retAddressId.getId(),Integer.getInteger(id),money,null,0,null,leaseOut));
            //model.addAttribute("retRentalInformationList",retRentalInformationList);
        }

        model.addAttribute("retAddressList",retAddressList);
        model.addAttribute("retCollectionList",retCollectionList);
        return "SearchList.html";
    }

    @RequestMapping(value = "/tiaoJian")
    public String tiaoJian(Integer CSId,Integer regionId,Integer money,String retWei,Integer huxingId,String pandu,String leaseOut,Model model){
        //查询出所以的收藏状态为0:收藏中....
        List<RetCollection> retCollectionList = retCollectionDao.selectList(new QueryWrapper<RetCollection>().eq("ret_state",0));
        List<Map<String, Object>> tiaoJianList = ccc(CSId,regionId,money,retWei,huxingId,pandu,leaseOut);
        model.addAttribute("retCollectionList",retCollectionList);
        model.addAttribute("List",tiaoJianList);
        return "SearchList::baise";
    }

    @RequestMapping(value = "/zonshu")
    public String zonshu(Integer CSId,String regionId,Integer money,String retWei,String huxingId,String leaseOut,Model model){
        List<RetRentalInformation> retRentalInformationList = null;
        List<RetRentalInformation> fanYuancount =  null;

        if(money == null){
            money = 0;
        }
        if(CSId != null && regionId  == "" && retWei == "" &&  huxingId == "" && leaseOut == null && money == 0){
            retRentalInformationList = retRentalInformationDao.selectList(new QueryWrapper<RetRentalInformation>().like("address_Id",CSId).groupBy("lease_out"));
            fanYuancount = retRentalInformationDao.selectList(new QueryWrapper<RetRentalInformation>().like("address_Id",CSId));
        }else{
            if (leaseOut == null){
                leaseOut = "";
            }
            //获取总楼盘
            if(money != null && money != 0){
                retRentalInformationList = retRentalInformationDao.selectList(new QueryWrapper<RetRentalInformation>().like("address_Id",CSId).like("region_Id",regionId).lt("money",money).like("ret_wei",retWei).like("huxing_Id",huxingId).like("lease_out",leaseOut).groupBy("region_Id"));
            }else{
                retRentalInformationList = retRentalInformationDao.selectList(new QueryWrapper<RetRentalInformation>().like("address_Id",CSId).like("region_Id",regionId).like("ret_wei",retWei).like("huxing_Id",huxingId).like("lease_out",leaseOut).groupBy("region_Id"));
            }
            //获取总房源
            if(money != null && money != 0){
                fanYuancount = retRentalInformationDao.selectList(new QueryWrapper<RetRentalInformation>().like("address_Id",CSId).like("region_Id",regionId).lt("money",money).like("ret_wei",retWei).like("huxing_Id",huxingId).like("lease_out",leaseOut));
            }else{
                fanYuancount = retRentalInformationDao.selectList(new QueryWrapper<RetRentalInformation>().like("address_Id",CSId).like("region_Id",regionId).like("ret_wei",retWei).like("huxing_Id",huxingId).like("lease_out",leaseOut));
            }
        }
        model.addAttribute("retRentalInformationList",retRentalInformationList.size());
        model.addAttribute("fanYuancount",fanYuancount.size());
        return "SearchList::shen";
    }




    public  List<Map<String, Object>> ccc(Integer CSId,Integer regionId,Integer money,String retWei,Integer huxingId,String pandu,String leaseOut){
        //创建一个字典集合
        List<Map<String, Object>> jsonoList = new ArrayList<Map<String, Object>>();

        List<Map<String, Object>> retRentalInformationList = null;
        QueryWrapper<RetRentalInformation> wrapper = new QueryWrapper();

        if(CSId != null && CSId != 0){
            wrapper.eq("c.address_Id", CSId);
        }
        if(regionId != null && regionId != 0){
            wrapper.eq("c.region_Id", regionId);
        }
        if(retWei != "" && retWei != null){
            wrapper.eq("c.ret_wei", retWei);
        }
        if(money != null && money != 0){
            wrapper.lt("c.money", money);
        }
        if(huxingId != null && huxingId != 0){
            wrapper.eq("c.huxing_Id", huxingId);
        }
        if(leaseOut != null && leaseOut != ""){
            wrapper.like("c.lease_out", leaseOut);
        }
        if(pandu != null && pandu != "" && pandu=="金钱"){
            wrapper.orderByAsc("money");
        }
        retRentalInformationList = retRentalInformationDao.test(wrapper);


        for (Map map : retRentalInformationList){
            Map<String, Object> hashMap = new HashMap<>();

            hashMap.put("house_id",map.get("ret_id"));
            hashMap.put("lease_out",map.get("lease_out"));
            hashMap.put("ch_cityName",map.get("ch_cityName"));
            hashMap.put("cityName",map.get("cityName"));
            hashMap.put("ret_distance",map.get("ret_jiaoton"));
            hashMap.put("money",map.get("money"));
            hashMap.put("ret_picture",map.get("ret_picture"));
            hashMap.put("ret_lng",map.get("ret_lng"));
            hashMap.put("ret_lat",map.get("ret_lat"));
            hashMap.put("xiao",retRegionList(CSId.toString(),map.get("region_Id").toString(),regionId,money,retWei,huxingId));

            jsonoList.add(hashMap);
        }

        return jsonoList;
    }

    public List<RetRentalInformation> retRegionList(String id,String fanYuan,Integer regionId,Integer money,String retWei,Integer huxingId){
        QueryWrapper<RetRentalInformation> wrapper = new QueryWrapper();
        if(id != null && id != ""){
            wrapper.eq("address_Id", id);
        }
        if(fanYuan != null && fanYuan != ""){
            wrapper.eq("region_Id", fanYuan);
        }
        if(regionId != null && regionId != 0){
            wrapper.eq("region_Id", regionId);
        }
        if(retWei != null && retWei != ""){
            wrapper.eq("ret_wei", retWei);
        }
        if(money != null && money != 0){
            wrapper.lt("money", money);
        }
        if(huxingId != null && huxingId != 0){
            wrapper.eq("huxing_Id", huxingId);
        }
        List<RetRentalInformation> retRegionList = retRentalInformationDao.Wrapper(wrapper);
        return retRegionList;
    }

    @RequestMapping(value = "/ditu")
    public String ditu(Model model,RetRentalInformation retRentalInformation){
        //无条件情况下获取所以的城市名称
        List<RetAddress> retAddressList = retAddressdao.selectList(null);

        //根据点击房源传过来的id来查询相对应的房源基本信息
        List<Map<String,Object>> retRentalInformationList = retRentalInformationDao.FanyuanSelect(retRentalInformation.getRetId());

        if(sid != null && sid != 0){
            //根据id获取相对应的城市名称
            RetAddress retAddressId = retAddressdao.selectOne(new QueryWrapper<RetAddress>().eq("id",sid));
            model.addAttribute("quyu",lll(retAddressId.getId()));

            model.addAttribute("quyuList",quyu(retAddressId.getId(),"",0,"",""));
            //城市id
            model.addAttribute("cityId",retAddressId.getId());
            //城市名称
            model.addAttribute("cityName",retAddressId.getCityName());
            //经度
            model.addAttribute("lng",retAddressId.getAddressLongitude());
            //纬度
            model.addAttribute("lat",retAddressId.getAddressLatitude());
        }
        model.addAttribute("retAddressList",retAddressList);
        model.addAttribute("retRentalInformationList",retRentalInformationList);
        return "ditu";
    }

    //根据条件显示相关地区的房源
    @RequestMapping(value = "/fanyuanList")
    @ResponseBody
    public String fanyuanList(Integer CSId,String huxingId,Integer money,String retWei,String leaseOut){
        List<Map<String,Object>> retRentalInformationList = quyu(CSId,huxingId,money,retWei,leaseOut);
        return JSONArray.toJSONString(retRentalInformationList);
    }

    //根据id显示相关城市上的区
    @RequestMapping(value = "/retPropertiesList")
    @ResponseBody
    public String retPropertiesList(Integer regionId,String qianZhi,String huxingId,Integer money,String retWei,String leaseOut){
        List<Map<String,Object>> retRentalInformationList = null;
        if(qianZhi != null && qianZhi != ""){
            retRentalInformationList = xiangzheng(null,qianZhi,huxingId,money,retWei,leaseOut);
        }else if (regionId != null && regionId != 0){
            retRentalInformationList =xiangzheng(regionId,null,huxingId,money,retWei,leaseOut);
        }
        return JSONArray.toJSONString(retRentalInformationList);
    }

    //根据id显示相关区域上的楼盘
    @RequestMapping(value = "/retRentalInformationList")
    @ResponseBody
    public String retRentalInformationList(Integer addressId,String leaseOut,String shenZhi,String huxingId,Integer money,String retWei,String leaseOut1){
        List<Map<String,Object>> retRentalInformationList= null;
        if(addressId != 0 && leaseOut != null && leaseOut !=""){
            retRentalInformationList =lonpan(addressId,leaseOut,null,huxingId,money,retWei,leaseOut1);
        }else if(shenZhi != null && shenZhi != ""){
            retRentalInformationList =lonpan(0,null,shenZhi,huxingId,money,retWei,leaseOut1);
        }else{
            retRentalInformationList =lonpan(addressId,null,null,huxingId,money,retWei,leaseOut1);
        }
        return JSONArray.toJSONString(retRentalInformationList);
    }

    @RequestMapping(value = "/chanquyu")
    @ResponseBody
    public String chanquyu(Integer addressId,String leaseOut){
        String c = "%"+leaseOut+"%";
        List<Map<String,Object>> chanquyu =retRentalInformationDao.chanquyu(addressId,c);;
        return JSONArray.toJSONString(chanquyu);
    }

    @RequestMapping(value = "/leaseOut")
    @ResponseBody
    public String retId(String leaseOut){
        List<Map<String,Object>> retRentalInformationList = retRentalInformationDao.leaseOutSelect(leaseOut);
        return JSONArray.toJSONString(retRentalInformationList);
    }

    //根据id显示相关的区域信息
    public  List<Map<String, Object>> quyu(Integer CSId,String huxingId,Integer money,String retWei,String leaseOut){
        //创建一个字典集合
        List<Map<String, Object>> jsonoList = new ArrayList<Map<String, Object>>();

        List<RetRegion> retRegionList = null;

        if(CSId != null ){
            retRegionList = retRegionDao.selectList(new QueryWrapper<RetRegion>().eq("address_Id",CSId));
        }

        Map<String, Object> hashMap = null;

        for (RetRegion retRegion : retRegionList){
            hashMap = new HashMap<>();
            hashMap.put("id",retRegion.getId());
            hashMap.put("cityName",retRegion.getCityName());
            hashMap.put("address_Id",retRegion.getAdderssId());
            hashMap.put("regionLongitude",retRegion.getRegionLongitude());
            hashMap.put("regionLatitude",retRegion.getRegionLatitude());
            hashMap.put("count",retRegionList(retRegion.getId(),huxingId,money,retWei,leaseOut));
            jsonoList.add(hashMap);
        }
        return jsonoList;
    }
    public Integer retRegionList(Integer id,String huxingId,Integer money,String retWei,String leaseOut){
        QueryWrapper<RetRentalInformation> wrapper = new QueryWrapper();
        if(id != null && id != 0){
            wrapper.eq("region_Id", id);
        }
        if(huxingId != null && huxingId != ""){
            wrapper.eq("huxing_Id", huxingId);
        }
        if(money != null && money != 0){
            wrapper.lt("money", money);
        }
        if(retWei != null && retWei != ""){
            wrapper.eq("ret_wei", retWei);
        }
        if(leaseOut != null && leaseOut != ""){
            wrapper.like("lease_out", leaseOut);
        }
        Integer users = retRentalInformationDao.selectByMyWrapper(wrapper);
        if (users == null){
            users = 0;
        }
        return users;
    }


    //根据id显示相关的乡镇信息
    public  List<Map<String, Object>> xiangzheng(Integer CSId,String qianZhi,String huxingId,Integer money,String retWei,String leaseOut){
        //创建一个字典集合
        List<Map<String, Object>> jsonoList = new ArrayList<Map<String, Object>>();

        List<RetProperties> retRegionList = null;

        if(qianZhi != null){
            retRegionList = retPropertiesDao.xianShiList(qianZhi);
        }else{
            retRegionList = retPropertiesDao.retPropertiesList(CSId);
        }

        Map<String, Object> hashMap = null;

        for (RetProperties retProperties : retRegionList){
            hashMap = new HashMap<>();
            hashMap.put("id",retProperties.getId());
            hashMap.put("properties",retProperties.getProperties());
            hashMap.put("regionId",retProperties.getRegionId());
            hashMap.put("propertiesLongitude",retProperties.getPropertiesLongitude());
            hashMap.put("propertiesLatitude",retProperties.getPropertiesLatitude());
            hashMap.put("count",xiangZhenList(retProperties.getId(),huxingId,money,retWei,leaseOut));
            jsonoList.add(hashMap);
        }
        return jsonoList;
    }
    public Integer xiangZhenList(Integer id,String huxingId,Integer money,String retWei,String leaseOut){
        QueryWrapper<RetRentalInformation> wrapper = new QueryWrapper();
        if(id != null && id != 0){
            wrapper.eq("ret_propertiesId", id);
        }
        if(huxingId != null && huxingId != ""){
            wrapper.eq("huxing_Id", huxingId);
        }
        if(money != null && money != 0){
            wrapper.lt("money", money);
        }
        if(retWei != null && retWei != ""){
            wrapper.eq("ret_wei", retWei);
        }
        if(leaseOut != null && leaseOut != ""){
            wrapper.like("lease_out", leaseOut);
        }
        //Integer retRegionList = retRentalInformationDao.selectCount(new QueryWrapper<RetRentalInformation>().eq("",id));
        Integer retRegionList = retRentalInformationDao.selectByMyWrapper(wrapper);
        if (retRegionList == null){
            retRegionList = 0;
        }
        return retRegionList;
    }


    //根据id显示相关的楼盘信息
    public  List<Map<String, Object>> lonpan(Integer CSId,String leaseOut,String shenZhi,String huxingId,Integer money,String retWei,String leaseOut1){
        //创建一个字典集合
        List<Map<String, Object>> jsonoList = new ArrayList<Map<String, Object>>();

        List<RetRentalInformation> retRentalInformationList = null;

        if(CSId != 0 && CSId != null){
            retRentalInformationList = retRentalInformationDao.selectList(new QueryWrapper<RetRentalInformation>().eq("address_Id",CSId).groupBy("lease_out"));
        }else if(CSId != 0 && leaseOut != null && CSId != null ){
            String c = "%"+leaseOut+"%";
            retRentalInformationList = retRentalInformationDao.retRentalInformationList(CSId,c);
        }else{
            retRentalInformationList = retRentalInformationDao.lou(shenZhi);
        }

        Map<String, Object> hashMap = null;

        for (RetRentalInformation retRegionList : retRentalInformationList){
            hashMap = new HashMap<>();
            hashMap.put("id",retRegionList.getRetId());
            hashMap.put("leaseOut",retRegionList.getLeaseOut());
            hashMap.put("retLng",retRegionList.getRetLng());
            hashMap.put("retLat",retRegionList.getRetLat());
            hashMap.put("count",lonpanList(retRegionList.getLeaseOut(),huxingId,money,retWei,leaseOut1));
            jsonoList.add(hashMap);
        }
        return jsonoList;
    }
    public Integer lonpanList(String leaseOut,String huxingId,Integer money,String retWei,String leaseOut1){
        QueryWrapper<RetRentalInformation> wrapper = new QueryWrapper();
        if(leaseOut != null && leaseOut != ""){
            wrapper.eq("lease_out", leaseOut);
        }
        if(huxingId != null && huxingId != ""){
            wrapper.eq("huxing_Id", huxingId);
        }
        if(money != null && money != 0){
            wrapper.lt("money", money);
        }
        if(retWei != null && retWei != ""){
            wrapper.eq("ret_wei", retWei);
        }
        if(leaseOut1 != null && leaseOut1 != ""){
            wrapper.like("lease_out", leaseOut1);
        }
        //Integer retRegionList = retRentalInformationDao.selectCount(new QueryWrapper<RetRentalInformation>().eq("lease_out",leaseOut));
        Integer retRegionList = retRentalInformationDao.selectByMyWrapper(wrapper);
        if (retRegionList == null){
            retRegionList = 0;
        }
        return retRegionList;
    }

    @RequestMapping(value = "/lll")
    @ResponseBody
    public String lll(){
        List<Map<String,Object>> retRentalInformationList = lll(1);
        return JSONArray.toJSONString(retRentalInformationList);
    }



    public  List<Map<String, Object>> lll(Integer CSId){
        //创建一个字典集合
        List<Map<String, Object>> jsonoList = new ArrayList<Map<String, Object>>();

        List<RetRegion> retRegions = null;

        if(CSId != null ){
            retRegions = retRegionDao.selectList(new QueryWrapper<RetRegion>().eq("address_Id",CSId));
        }

        Map<String, Object> hashMap = null;

        for (RetRegion retRegion: retRegions){
            hashMap = new HashMap<>();
            hashMap.put("id",retRegion.getId());
            hashMap.put("cityName",retRegion.getCityName());
            hashMap.put("regionLongitude",retRegion.getRegionLongitude());
            hashMap.put("regionLatitude",retRegion.getRegionLatitude());
            hashMap.put("xiao",nhao(retRegion.getId()));
            jsonoList.add(hashMap);
        }
        return jsonoList;
    }

    public  List<RetProperties> nhao(Integer addesserId){
        List<RetProperties> retPropertiesList = retPropertiesDao.selectList(new QueryWrapper<RetProperties>().eq("region_id",addesserId));
        return retPropertiesList;
    }

    @RequestMapping(value = "/duanxin")
    @ResponseBody
    public String duanxin(String phone){
        String fanhui = null;
        // 生成随机的六位数验证码
        Random random = new Random();
        verifyCode = random.nextInt(10000);
        SmsParams smsParams = new SmsParams(phone,verifyCode.toString());
        System.out.println(smsParams.getPhone());
        String daunx = txCloudSmsUtil.sendSms(smsParams);
        if (daunx == "success"){
            fanhui = "0";
        }else{
            fanhui="1";
        }
        return JSONArray.toJSONString(fanhui);
    }

    //单点预定看房
    @RequestMapping(value = "/yuding")
    @ResponseBody
    public String yuding(String fu_mobile,String rentalId,String biaoshi){
        String fanhui = null;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        SysBooking sysBooking = new SysBooking();
        if(biaoshi.equals("true")) {
            int count = sysBookingDao.selectCount(new QueryWrapper<SysBooking>().eq("phone",fu_mobile).eq("rental_id",rentalId).in("rental_stateid",17,18));
            if(count <= 0){
                sysBooking.setPhone(fu_mobile);
                sysBooking.setRentalId(rentalId);
                sysBooking.setAppointmentTime(df.format(new Date()));
                sysBooking.setRentalStateid("18");
                int size = sysBookingDao.insert(sysBooking);
                if (size > 0) {
                    RetUser retUsers = retUserDao.selectOne(new QueryWrapper<RetUser>().eq("phone", fu_mobile));
                    fanhui = retUsers.getPhone();
                } else {
                    fanhui = "1";
                }
            }else{
                fanhui =  "3";
            }
        }else{
            fanhui = "1";
        }
        return JSONArray.toJSONString(fanhui);
    }

    //手机账号验证码登录
    @RequestMapping(value = "/adduser")
    @ResponseBody
    public String adduser(HttpSession session,String fu_mobile,String vcode,String biaoshi,String rentalId){
        String fanhui = null;
        RetUser retUser = new RetUser();
        SysBooking sysBooking = new SysBooking();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        if(vcode.equals(verifyCode.toString())){
            int count = retUserDao.selectCount(new QueryWrapper<RetUser>().eq("phone",fu_mobile));
            if(count > 0){
                if(biaoshi.equals("true")){
                    sysBooking.setPhone(fu_mobile);
                    sysBooking.setRentalId(rentalId);
                    sysBooking.setAppointmentTime(df.format(new Date()));
                    sysBooking.setRentalStateid("18");
                    int size = sysBookingDao.insert(sysBooking);
                    if(size > 0){
                        RetUser retUsers = retUserDao.selectOne(new QueryWrapper<RetUser>().eq("phone",fu_mobile));
                        session.setAttribute("username",retUsers.getPhone());
                        session.setAttribute("userId",retUsers.getId());
                        fanhui = retUsers.getPhone();
                    }else{
                        fanhui = "1";
                    }
                }else{
                    RetUser retUsers = retUserDao.selectOne(new QueryWrapper<RetUser>().eq("phone",fu_mobile));
                    session.setAttribute("username",retUsers.getPhone());
                    session.setAttribute("userId",retUsers.getId());
                    fanhui = retUsers.getPhone();
                }
            }else{
                retUser.setPhone(fu_mobile);
                int size = retUserDao.insert(retUser);
                if(size > 0){
                    session.setAttribute("username",retUser.getPhone());
                    int shu = retUserDao.maxId();
                    session.setAttribute("userId",shu);
                    fanhui = retUser.getPhone();
                }else{
                    fanhui = "1";
                }
            }
        }else{
            fanhui = "2";
        }
        return JSONArray.toJSONString(fanhui);
    }

    //手机账号密码登录
    @RequestMapping(value = "/denglogin")
    @ResponseBody
    public String denglogin(String fu_mobile,String fu_password,HttpSession session){
        String fanhui = null;
        int count = retUserDao.selectCount(new QueryWrapper<RetUser>().eq("phone",fu_mobile).eq("password",fu_password));
        if(count > 0){
            fanhui = "0";
            RetUser retUser = retUserDao.selectOne(new QueryWrapper<RetUser>().eq("phone",fu_mobile).eq("password",fu_password));
            session.setAttribute("username",fu_mobile);
            session.setAttribute("userId",retUser.getId());
        }else{
            fanhui = "1";
        }
        return JSONArray.toJSONString(fanhui);
    }

    //添加收藏/取消收藏
    @RequestMapping(value = "/souchen")
    @ResponseBody
    public String souchen(String userId,String rentalId,Integer id){
        String fanhui = null;
        int count = retCollectionDao.selectCount(new QueryWrapper<RetCollection>().eq("user_Id",userId).eq("rental_Id",rentalId).eq("ret_state",0));
        if(count > 0){
            RetCollection retCollection = new RetCollection();
            retCollection.setId(id);
            retCollection.setUserId(userId);
            retCollection.setRentalId(rentalId);
            retCollection.setRetState("1");
            int zhi = retCollectionDao.updateById(retCollection);
            if(zhi > 0){
                fanhui = "取消成功";
            }else{
                fanhui = "取消失败";
            }
        }else{
            RetCollection retCollection = new RetCollection();
            retCollection.setUserId(userId);
            retCollection.setRentalId(rentalId);
            retCollection.setRetState("0");
            int shu = retCollectionDao.insert(retCollection);
            if(shu > 0){
                fanhui = "收藏成功";
            }else{
                fanhui = "收藏失败";
            }
        }
        return JSONArray.toJSONString(fanhui);
    }

    //用户的退出
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("username");
        session.removeAttribute("userId");
        return "redirect:/retAddress/index";
    }

    @RequestMapping(value = "/test")
    public String test(){
        return "test";
    }
}
